package com.hhxy.config;

import com.hhxy.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author ghp
 * @date 2023/3/8
 * @title SpringSecurity配置类
 * @description 用于配置SpringSecurity相关的一些配置
 */
@Configuration
// 开启基于注解的安全认证机制
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    /**
     * 使用BCryptPasswordEncoder进行加密（SpringSecurity默认是使用PasswordEncoder进行加密）
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 将AuthenticationManager注入容器
     */
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    /**
     * 相关配置
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 初始化配置
        http.csrf().disable() // 关闭csrf
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 不通过Session获取SecurityContext
                .and()
                .authorizeRequests()
                .antMatchers("/user/login").anonymous() // 对于登录接口, 允许匿名访问
                .anyRequest().authenticated(); // 除上面外的所有请求全部需要鉴权认证

        // 这个没啥用，主要是讲解用的
//        http.csrf().disable() // 关闭csrf
//                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 关闭Session
//                .and()
//                .authorizeRequests()
//                .antMatchers("/hello").permitAll() // /hello/** 请求都进行放行
//                .antMatchers("/user/login").anonymous() // 对于登录接口, 允许匿名访问    .
//                .anyRequest().anonymous(); // 除上面那些放行请求外都需要进行鉴权认证

        // 配置过滤器，将我们自定义的过滤器放在UsernamePasswordAuthenticationFilter之前
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        // 配置异常处理器
        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint) // 注入自定义的认证失败异常处理器
                .accessDeniedHandler(accessDeniedHandler); // 注入自定义的权限不足异常处理器

        //允许跨域
        http.cors();
    }
}
